package com.suirich.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suirich.bean.Account;
import com.suirich.common.Constants;
import com.suirich.service.AccountService;

@Controller
public class UserController {
	
	
	@Autowired
	private AccountService accountService;

    @RequestMapping(value="/account/login",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> login(Account loginUser,HttpServletRequest request){

        List<Account> accountList = this.accountService.read(loginUser);

        Map<String,String> result = new HashMap<String,String>();
        
        if(StringUtils.isNotEmpty(loginUser.getUserName()) && CollectionUtils.isNotEmpty(accountList) && accountList.size()>0){
            Account account = accountList.get(0);
            
            request.getSession().setAttribute(Constants.USER_ID_IN_SESSION, account.getId());
            request.getSession().setAttribute(Constants.USER_NAME_IN_SESSION, account.getUserName());

            result.put("result", Constants.SUCCESS_RESULT_FLAG);
            result.put("redirectUrl", "home");  
        }else{
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        
        return result;
    }
    
    @RequestMapping(value="/account/logout",method=RequestMethod.GET)
	public String logout(HttpServletRequest request){

        request.getSession().invalidate();

        
        return "redirect:/admin";
	}
    
    @RequestMapping(value="/admin/home")
    public ModelAndView home(ModelMap modelMap,HttpServletRequest request){
        
        modelMap.put("userName", request.getSession().getAttribute(Constants.USER_NAME_IN_SESSION));
        ModelAndView mav = new ModelAndView("/admin/homePage.jsp", modelMap);
        
        return mav;
    }

    @RequestMapping(value="/account/modifyPassword",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,String> modifyPassword(String originalPassword,String newPassword){

        Account account = this.accountService.read(1L);

        Map<String,String> result = new HashMap<String,String>();
        
        if(account.getPassword().equals(originalPassword)){
            account.setPassword(newPassword);
            
            this.accountService.update(account);

            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        }else{
            result.put("result", Constants.FAILURE_RESULT_FLAG);
            result.put("tips", "The Original Password that your input is error,please try again!");
        }
        
        
        return result;
    }
}
